//
//  ViewController.swift
//  SwiftSign
//
//  Created by continue on 2021/4/23.
//

import UIKit
import SnapKit
import RxSwift
import RxCocoa
import SwifterSwift
import Toast_Swift

class ViewController: UIViewController, Navigatable {

    var viewModel: ViewModel?
    var navigator: Navigator!
    let error = PublishSubject<ApiError>()
    let isLoading = BehaviorRelay(value: false)
    
    init(viewModel: ViewModel?, navigator: Navigator) {
        self.viewModel = viewModel
        self.navigator = navigator
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        super.init(nibName: nil, bundle: nil)
    }
    
    var contentWidth: CGFloat {
        return Configs.BaseDimensions.baseContentWidth.auto()
    }
    
    var contentHeight: CGFloat {
        return Configs.BaseDimensions.baseContentHeight.auto()
    }
    
    var contentRadius: CGFloat {
        return Configs.BaseDimensions.cornerRadius.auto()
    }
    
    lazy var contentView: View = {
        let view = View()
        view.layer.cornerRadius = contentRadius
        view.backgroundColor = .white
        self.view.addSubview(view)
        view.snp.makeConstraints { (make) in
            make.center.equalTo(self.view.center)
            make.size.equalTo(CGSize(width: contentWidth, height: contentHeight))
        }
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        makeUI()
        bindViewModel()
    }

    func makeUI() {
        
    }
    
    func bindViewModel() {
        guard let viewModel = viewModel else { return }
        viewModel.parsedError.asObservable().bind(to: error).disposed(by: rx.disposeBag)
        error.subscribe(onNext: { [weak self] (error) in
            var title = ""
            switch error {
            case .serverError(let response):
                title = response.message ?? ""
            }
            self?.view.makeToast(title, position: .center)
        }).disposed(by: rx.disposeBag)
    }
}

